﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using TLL.Log;
using TLL.UTL;

namespace TasksLog
{
    public partial class TaskDetail : Form
    {
        private LogItem item = null;
        public TaskDetail(LogItem item)
        {
            this.item = item;
            InitializeComponent();
            Initialize();
        }
        public void Initialize()
        {
            var ids = new List<int>();
            if (this.item.Log != null)
                foreach (var item in this.item.Log)
                    ids.Add(item.ID);

            if (this.item.LogChanges != null)
                foreach (var item in this.item.LogChanges)
                    ids.Add(item.ID);

            ids = ids.OrderBy(x => x).ToList();

            LbNumber.Text = this.item.Text.Split(':')[0];
            LbText.Text = this.item.Text.Split(':')[1];

            foreach (var id in ids)
            {
                var logItem = this.item.HasLog ? this.item.Log.FirstOrDefault(x => x.ID == id) : null;
                var logChangeItem = this.item.HasLogChange ? this.item.LogChanges.FirstOrDefault(x => x.ID == id) : null;
                if (logItem == null && logChangeItem == null) continue;
                if (logItem != null)
                {
                    LBDate.Items.Add(logItem.Date.Value.ToString("d.M.yyyy HH:mm:ss"));
                    LBTarget.Items.Add("Time");
                    LBValue.Items.Add(LogTime.Formated(logItem.Time));
                }
                if (logChangeItem != null)
                {
                    LBDate.Items.Add(logChangeItem.Date.Value.ToString("d.M.yyyy HH:mm:ss"));
                    LBTarget.Items.Add(logChangeItem.Target);
                    LBValue.Items.Add(logChangeItem.Value);
                }
            }

            var countDays = Utilities.Round((this.item.DeadLine.Value - this.item.Create).TotalDays, RoundType.ToHigher);
            for (var i = 0; i < countDays + 1; i++ )
                MCDate.AddBoldedDate(this.item.Create.AddDays(i));

            MCDate.SelectionRange = new SelectionRange(this.item.Create, this.item.DeadLine.Value);

            if (this.item.Complite != null)
            {
                MCComplite.AddBoldedDate(this.item.Complite.Value);
                MCComplite.SetDate(this.item.Complite.Value);
            }
            else
            {
                LbComplite.Text = string.Empty;
                MCComplite.Visible = false;
            }
        }
        private void SelectIndex(int index)
        {
            LBDate.SelectedIndex = index;
            LBTarget.SelectedIndex = index;
            LBValue.SelectedIndex = index;
        }

        private void LB_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelectIndex((sender as ListBox).SelectedIndex);
        }
        private void LBValue_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index != -1)
            {
                e.DrawBackground();
                Brush myBrush = Brushes.Black;
                var value = LBValue.Items[e.Index].ToString();
                if (value == LogStatuses.Complite.ToString() || value == LogStatuses.Wait.ToString() || value == LogStatuses.Error.ToString())
                    switch (Utilities.GetEnum<LogStatuses>(value))
                    {
                        case LogStatuses.Complite: myBrush = Brushes.Green; break;
                        case LogStatuses.Wait: myBrush = Brushes.Blue; break;
                        case LogStatuses.Error: myBrush = Brushes.Red; break;
                    }

                e.Graphics.DrawString(LBValue.Items[e.Index].ToString(),
                    e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
                e.DrawFocusRectangle();
            }
        }

        private void BtClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
